﻿// Decompiled with JetBrains decompiler
// Type: System.Web.Razor.Parser.CallbackVisitor
// Assembly: System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// MVID: 682513D6-F37E-48AC-9909-F3EE103CFF14
// Assembly location: D:\Project\Idea\无人机外网\uav\客户端工程\排查工具\bin\Release\System.Web.Razor.dll

using System.Threading;
using System.Web.Razor.Parser.SyntaxTree;

namespace System.Web.Razor.Parser
{
  /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code. Represents a visitor that executes a callback upon the completion of a visit.</summary>
  public class CallbackVisitor : ParserVisitor
  {
    private Action<Span> _spanCallback;
    private Action<RazorError> _errorCallback;
    private Action<BlockType> _endBlockCallback;
    private Action<BlockType> _startBlockCallback;
    private Action _completeCallback;

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code. Initializes a new instance of the <see cref="T:System.Web.Razor.Parser.CallbackVisitor" /> class.</summary>
    /// <param name="spanCallback">The delegate for the span visit.</param>
    public CallbackVisitor(Action<Span> spanCallback)
      : this(spanCallback, (Action<RazorError>) (_ => { }))
    {
    }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code. Initializes a new instance of the <see cref="T:System.Web.Razor.Parser.CallbackVisitor" /> class.</summary>
    /// <param name="spanCallback">The delegate for the span visit.</param>
    /// <param name="errorCallback">The delegate for the error visit.</param>
    public CallbackVisitor(Action<Span> spanCallback, Action<RazorError> errorCallback)
      : this(spanCallback, errorCallback, (Action<BlockType>) (_ => { }), (Action<BlockType>) (_ => { }))
    {
    }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code. Initializes a new instance of the <see cref="T:System.Web.Razor.Parser.CallbackVisitor" /> class.</summary>
    /// <param name="spanCallback">The delegate for the span visit.</param>
    /// <param name="errorCallback">The delegate for the error visit.</param>
    /// <param name="startBlockCallback">The delegate for the start block visit.</param>
    /// <param name="endBlockCallback">The delegate for the end block visit.</param>
    public CallbackVisitor(
      Action<Span> spanCallback,
      Action<RazorError> errorCallback,
      Action<BlockType> startBlockCallback,
      Action<BlockType> endBlockCallback)
      : this(spanCallback, errorCallback, startBlockCallback, endBlockCallback, (Action) (() => { }))
    {
    }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code. Initializes a new instance of the <see cref="T:System.Web.Razor.Parser.CallbackVisitor" /> class.</summary>
    /// <param name="spanCallback">The delegate for the span visit.</param>
    /// <param name="errorCallback">The delegate for the error visit.</param>
    /// <param name="startBlockCallback">The delegate for the start block visit.</param>
    /// <param name="endBlockCallback">The delegate for the end block visit.</param>
    /// <param name="completeCallback">The delegate to execute for the complete event.</param>
    public CallbackVisitor(
      Action<Span> spanCallback,
      Action<RazorError> errorCallback,
      Action<BlockType> startBlockCallback,
      Action<BlockType> endBlockCallback,
      Action completeCallback)
    {
      this._spanCallback = spanCallback;
      this._errorCallback = errorCallback;
      this._startBlockCallback = startBlockCallback;
      this._endBlockCallback = endBlockCallback;
      this._completeCallback = completeCallback;
    }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code. Gets or sets the synchronization context for this callback visitor.</summary>
    /// <returns>The synchronization context for this callback visitor.</returns>
    public SynchronizationContext SynchronizationContext { get; set; }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code. Executes the visitor callback to visit the start block.</summary>
    /// <param name="block">The start block to visit.</param>
    public override void VisitStartBlock(System.Web.Razor.Parser.SyntaxTree.Block block)
    {
      base.VisitStartBlock(block);
      CallbackVisitor.RaiseCallback<BlockType>(this.SynchronizationContext, block.Type, this._startBlockCallback);
    }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code. Executes the visitor callback to visit the span.</summary>
    /// <param name="span">The span to visit.</param>
    public override void VisitSpan(Span span)
    {
      base.VisitSpan(span);
      CallbackVisitor.RaiseCallback<Span>(this.SynchronizationContext, span, this._spanCallback);
    }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code. Executes the visitor callback to visit the end block.</summary>
    /// <param name="block">The end block to visit.</param>
    public override void VisitEndBlock(System.Web.Razor.Parser.SyntaxTree.Block block)
    {
      base.VisitEndBlock(block);
      CallbackVisitor.RaiseCallback<BlockType>(this.SynchronizationContext, block.Type, this._endBlockCallback);
    }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code. Executes the visitor callback to visit the error.</summary>
    /// <param name="err">The Razor error to visit.</param>
    public override void VisitError(RazorError err)
    {
      base.VisitError(err);
      CallbackVisitor.RaiseCallback<RazorError>(this.SynchronizationContext, err, this._errorCallback);
    }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code. </summary>
    public override void OnComplete()
    {
      base.OnComplete();
      CallbackVisitor.RaiseCallback<object>(this.SynchronizationContext, (object) null, (Action<object>) (_ => this._completeCallback()));
    }

    private static void RaiseCallback<T>(
      SynchronizationContext syncContext,
      T param,
      Action<T> callback)
    {
      if (callback == null)
        return;
      if (syncContext != null)
        syncContext.Post((SendOrPostCallback) (state => callback((T) state)), (object) param);
      else
        callback(param);
    }
  }
}
